<?php
/**
 * 积分管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-31
 */
namespace Admin\Controller;

class IntegralController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db       =new \Model\UserIntegralRecordModel();
        $this->db_rule  =new \Model\UserIntegralRuleModel();
    }
    
    /*
     * 积分记录列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.add_time")){
            $map['recordDate']  =array("egt",I("request.add_time"));
        }
        $map                    =$this->_search("UserIntegralRecord");
        if(I("request.userName")){
            $map['userId']      =A("Common")->getIds(I("request.userName"),"nickName","User");
        }
        if(I("request.recordtype")){
            $map['sourceId']  =A("Common")->getIds(I("request.recordtype"),"ruleName","UserIntegralRule");
        }           
        $sortBy         ='recordId';
        $sort           =desc;
        $IntegralList   =$this->_list($this->db,$map,$sortBy,$sort,"recordId");
        $this->assign("iList",$IntegralList);
        $this->display();
    }
    
    /*
     * 删除、批量删除积分记录
     * 
     * @return #
     */
    public function del(){
        $map['recordId']=I("request.id");
        if($this->db->del($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
    
    /*
     * 积分规则列表
     * 
     * @return #
     */
    public function ruleInt(){
        if(I("request.ruleDescribe")){
            $map['ruleDescribe']    =I("request.ruleDescribe");
        }
        if(I("request.ruleName")){
            $map['ruleName']        =I("request.ruleName");
        }
        $map            =$this->_search("UserIntegralRule");
        $sortBy         ='ruleId';
        $sort           =desc;
        $IntegralRule   =$this->_list($this->db_rule,$map,$sortBy,$sort,"ruleId");
        $this->assign("iRule",$IntegralRule);
        $this->display();
    }
    
    /*
     * 显示编辑规则
     * 
     * @return #
     */
    public function show(){
        if(I("request.id")){
            $intInfo   =$this->db_rule->find(I("request.id",'',"int"));
            $this->assign("intInfo",$intInfo);
        }
        $this->display();
    }
    
    /*
     * 编辑积分规则
     * 
     * @return #
     */
    public function edit(){
        if(I("ruleId")){
            $map['ruleId']      =I("request.ruleId",'','int');
        }
        if(I("request.ruleName")){
            $map['ruleName']    =I("request.ruleName",'','trim');
        }else{
            $this->error("工作名称不能为空");
        }
        if(I("ruleDescribe")){
            $map['ruleDescribe']=I("request.ruleDescribe",'','trim');
        }
        if(I('request.ruleIntegral')){
            $map['ruleIntegral']=I("request.ruleIntegral",'','int');
        }
        if(I("request.ruleFrequency")){
            $map['ruleFrequency']=I("request.ruleFrequency",'','int');
        }
        if($this->db_rule->edit($map)){
            $this->success("积分规则编辑成功");
        }else{
            $this->error("积分规则编辑失败");
        }
    }
    
    /*
     *删除、批量删除 积分规则
     * 
     * @return #
     */
    public function del_rule(){
        $map['ruleId']  =I("request.id");
        if($this->db_rule->del($map)){            
            $this->success("删除成功",U("index"));
        }else{
            $this->error("删除失败",U("index"));
        }
    }
}